package com.zys.sac.uc.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zys.sac.uc.entity.SacOrganization;
import com.zys.sac.uc.entity.SacRole;
import com.zys.sac.uc.entity.SacUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author junekzhong
 * @date 2023/12/25 11:08
 */
@Data
public class UserInfoVO extends SacUser implements com.zys.sac.core.entity.SacUser, Serializable {

    private List<SacRole> roleList;

    private List<SacOrganization> organizationList;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roleList)) {
            roleList.forEach(role -> authorities.add(new SimpleGrantedAuthority("ROLE_" + String.valueOf(role.getCode()))));
        }
        return authorities;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
